package com.jtlrm.ckd.mvp.model;

import com.base.sdk.base.model.BaseModel;
import com.base.sdk.base.net.LifeCycleEvent;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.hyphenate.chatuidemo.db.NewGroupDao;
import com.hyphenate.chatuidemo.domain.NewGroupEntity;
import com.jtlrm.ckd.entity.GroupEntity;
import com.jtlrm.ckd.entity.YingYangDetailData;
import com.jtlrm.ckd.entity.submitEntity.AddGroupMember;
import com.jtlrm.ckd.entity.submitEntity.CreateGroupData;
import com.jtlrm.ckd.net.RetrofitUtil;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Observer;
import io.reactivex.subjects.PublishSubject;

public class SuiFangModel extends BaseModel {

    NewGroupDao newGroupDao;

    public void queryHuanZheList(String doctorId, Observer observer, PublishSubject<LifeCycleEvent> lifecycleSubject) {
        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty("doctorId", doctorId);
        RetrofitUtil.composeToSubscribe(RetrofitUtil.getService().queryHuanZhe(jsonObject), observer, lifecycleSubject);
    }

    public void queryWorkerList(String doctorId, Observer observer, PublishSubject<LifeCycleEvent> lifecycleSubject) {
        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty("doctorId", doctorId);
        jsonObject.addProperty("roleType", 2); // 传2代表ckd
        RetrofitUtil.composeToSubscribe(RetrofitUtil.getService().queryWorker(jsonObject), observer, lifecycleSubject);
    }

    public void createGroup(CreateGroupData data, Observer observer, PublishSubject<LifeCycleEvent> lifecycleSubject) {
        RetrofitUtil.composeToSubscribe(RetrofitUtil.getService().createGroup(createBody(data)), observer, lifecycleSubject);
    }

    public void queryGroup(String userId, Observer observer, PublishSubject<LifeCycleEvent> lifecycleSubject) {
        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty("userId", userId);
        jsonObject.addProperty("roleType", 2); // 传2代表ckd
        RetrofitUtil.composeToSubscribe(RetrofitUtil.getService().queryGroup(jsonObject), observer, lifecycleSubject);
    }

    public void queryHospitalGroup(String userId, Observer observer, PublishSubject<LifeCycleEvent> lifecycleSubject) {
        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty("userId", userId);
        jsonObject.addProperty("roleType", 2); // 传2代表ckd
        RetrofitUtil.composeToSubscribe(RetrofitUtil.getService().queryHospitalGroup(jsonObject), observer, lifecycleSubject);
    }

    public void queryGroupMember(String groupId, Observer observer, PublishSubject<LifeCycleEvent> lifecycleSubject) {
        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty("groupId", groupId);
        RetrofitUtil.composeToSubscribe(RetrofitUtil.getService().queryGroupMember(jsonObject), observer, lifecycleSubject);
    }

    public void exitGroup(String groupId, String userId, Observer observer, PublishSubject<LifeCycleEvent> lifecycleSubject) {
        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty("groupId", groupId);
        jsonObject.addProperty("userId", userId); // 传2代表ckd
        RetrofitUtil.composeToSubscribe(RetrofitUtil.getService().exitGroup(jsonObject), observer, lifecycleSubject);
    }

    public void changeGroupName(String groupId, String groupName, Observer observer, PublishSubject<LifeCycleEvent> lifecycleSubject) {
        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty("id", groupId);
        jsonObject.addProperty("groupName", groupName);
        jsonObject.addProperty("desc", "");
        RetrofitUtil.composeToSubscribe(RetrofitUtil.getService().changeGroupName(jsonObject), observer, lifecycleSubject);
    }

    public void queryGroupDetail(String easemobGroupId, String userId, Observer observer, PublishSubject<LifeCycleEvent> lifecycleSubject) {
        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty("easemobGroupId", easemobGroupId);
        jsonObject.addProperty("userId", userId); // 传2代表ckd
        RetrofitUtil.composeToSubscribe(RetrofitUtil.getService().queryGroupDetail(jsonObject), observer, lifecycleSubject);
    }

    public void deleteGroupMember(String groupId, String userId, Observer observer, PublishSubject<LifeCycleEvent> lifecycleSubject) {
        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty("groupId", groupId);
        jsonObject.addProperty("userId", userId);
        RetrofitUtil.composeToSubscribe(RetrofitUtil.getService().deleteGroupMember(jsonObject), observer, lifecycleSubject);
    }

    public void addGroupMember(AddGroupMember addGroupMember, Observer observer, PublishSubject<LifeCycleEvent> lifecycleSubject) {
        RetrofitUtil.composeToSubscribe(RetrofitUtil.getService().addGroupMember(createBody(addGroupMember)), observer, lifecycleSubject);
    }

    public void queryUserInfoByIm(String easemobId, Observer observer, PublishSubject<LifeCycleEvent> lifecycleSubject) {
        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty("easemobId", easemobId);
        RetrofitUtil.composeToSubscribe(RetrofitUtil.getService().queryUserInfoByImId(jsonObject), observer, lifecycleSubject);
    }

    public void saveGroup(List<GroupEntity> data) {
        if (newGroupDao == null) {
            newGroupDao = new NewGroupDao();
        }
        try {
            List<NewGroupEntity> newGroupEntities = new ArrayList<>();
            for (GroupEntity entity : data) {
                NewGroupEntity newGroupEntity = new NewGroupEntity();
                newGroupEntity.setEasemobGroupId(entity.getEasemobGroupId());
                newGroupEntity.setName(entity.getName());
                newGroupEntity.setCreateTime(entity.getCreateTime());
                newGroupEntities.add(newGroupEntity);
            }
            newGroupDao.saveGroupList(newGroupEntities);
        } catch (Exception e) {
        }

    }
}
